﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SSOCommon;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Net;
using System.Web;

namespace SSOCommon
{
    public static class Tools
    {
        public static string SerializeData<T>(T instance) where T : new()
        {
            string returnValue = String.Empty;
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            Stream xmlContent = new MemoryStream();
            serializer.Serialize(xmlContent, instance);
            xmlContent.Seek(0, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(xmlContent))
            {
                returnValue = reader.ReadToEnd();
            }
            return returnValue;
        }

        public static T DeserializeData<T>(Stream data) where T : new()
        {
            T returnValue = default(T);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            object obj = serializer.Deserialize(data);
            if (obj == null)
            {
                return returnValue;
            }
            return (T)obj;

        }

        public static string SendRequest(string url)
        {
            string returnValue = String.Empty;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "text/html";
            Stream responseData = request.GetResponse().GetResponseStream();
            using (StreamReader reader = new StreamReader(responseData))
            {
                returnValue = reader.ReadToEnd();
            }
            return returnValue;
        }

        public static string Redirect(string returnUrl, Dictionary<string, object> queryArguments)
        {
            if (String.IsNullOrEmpty(returnUrl))
            {
                throw new ArgumentNullException("returnUrl");
            }
            string url = String.Format("{0}{1}", returnUrl, ConstructQueryString(queryArguments, returnUrl.IndexOf("?") > -1));
            StringBuilder script = new StringBuilder();
            script.AppendLine("<html>");
            script.AppendLine("<head>");
            script.AppendLine("<title></title>");
            script.AppendLine("<script type=\"text/javascript\">");
            script.AppendLine(String.Format("window.location.href=\"{0}\";", url));
            script.AppendLine("</script");
            script.AppendLine("</head>");
            script.AppendLine("<body>");
            script.AppendLine("</body>");
            script.AppendLine("</html>");
            return script.ToString();
        }

        public static string GenerateUrl(string url, Dictionary<string, object> queryArguments)
        {
            return String.Format("{0}{1}", url, ConstructQueryString(queryArguments, url.IndexOf("?") > -1));
        }

        public static string ConstructQueryString(Dictionary<string, object> arguments, bool isAppend)
        {
            if (arguments == null)
            {
                return String.Empty;
            }
            StringBuilder args = new StringBuilder();
            args.Append(String.Format("{0}1=1", isAppend ? "&" : "?").Trim());
            object temp = null;
            foreach (string name in arguments.Keys)
            {
                temp = arguments[name];
                if (temp == null || String.IsNullOrEmpty(temp.ToString()))
                {
                    continue;
                }
                args.Append(String.Format("&{0}={1}", name, temp.ToString()));
            }
            return args.ToString();
        }

        public static string GetParamValue(string key)
        {
            return String.IsNullOrEmpty(HttpContext.Current.Request[key]) ? String.Empty : HttpContext.Current.Request[key];
        }
    }
}
